package com.twu.biblioteca.menu;

import com.twu.biblioteca.Book;
import com.twu.biblioteca.Reserver;
import com.twu.biblioteca.user.User;

import java.io.IOException;

public class BookDetails extends MenuItem {

    private static final int CUSTOMER_ID = 100;
    private static final String OPTION_RESERVE = "R";
    private static final String PROMPT = "Type " + OPTION_RESERVE
            + " to reserve this book or any other key to go back.";
    private static final String FORMAT_BOOK_DETAILS = "Book ID:\t%d\nTitle:\t%s\n";

    private final Reserver reserver;
    private final MenuInputReader reader;
    private final Book book;

    public BookDetails(String title, User user, Reserver reserver, MenuInputReader reader, Book book)
    {
        super(title, user);
        this.reserver = reserver;
        this.reader = reader;
        this.book = book;
    }

    @Override
    public String toString() {
        String bookDetails = String.format(FORMAT_BOOK_DETAILS, book.getItemId(), book.hashCode());
        return bookDetails + PROMPT;
    }

    @Override
    public void run() {
        try
        {
            String input = reader.readString();
            if(input.equals(OPTION_RESERVE))
            {
                String result = reserver.reserveItem(book, CUSTOMER_ID);
                System.out.println(result);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
